<template>
	<div>
		<div class="app-container">
			<TopInfo />

			<Search :lookup="data.lookup" @investigate="inves" />

			<el-button v-prmis="'config_lang_add'" type="primary" size="small" @click="add">
				<template #default>
					<span class="text-sm">{{ $t('manage.language.add') }}</span>
				</template>
			</el-button>

		</div>

		<div class="app-container" v-loading="data.mtloading">
			<el-table :data="data.list" :header-cell-style="{ 'text-align': 'center' }" height="700"
				:cell-style="{ 'text-align': 'center' }" class="w-full">
				<el-table-column prop="l_id" label="ID" />
				<el-table-column prop="name" :label="$t('manage.language.name')" />
				<el-table-column prop="lang" :label="$t('machine.configs.langCode')" />

				<el-table-column prop="creator_nickname" :label="$t('source.creater')">
					<template #default="scope">
						{{ scope.row.creator_nickname || '--' }}
					</template>
				</el-table-column>

				<el-table-column prop="create_time" :label="$t('date.createTime')">
					<template #default="scope">
						{{ getPlevel.gettime(scope.row.create_time, 3) }}
					</template>
				</el-table-column>

				<el-table-column prop="update_time" :label="$t('date.modifyTime')">
					<template #default="scope">
						{{ getPlevel.gettime(scope.row.update_time, 3) }}
					</template>
				</el-table-column>

				<el-table-column :label="$t('public.operate')">
					<template #default="scope">
						<el-button v-prmis="'config_lang_update'" type="primary" size="small"
							@click="modify(scope.row)"><el-icon>
								<EditPen />
							</el-icon></el-button>
						<el-button v-prmis="'config_lang_del'" type="danger" size="small"
							@click="del(scope.row.l_id)"><el-icon>
								<Delete />
							</el-icon></el-button>
					</template>
				</el-table-column>
			</el-table>

			<PageNum :total="data.total" :page="data.lang" @exposeData="current" @fraction="fraction" />
		</div>

		<el-dialog v-model="data.dialogVisible" :title="$t('manage.language.edit')" center width="30%">
			<el-form v-if="data.dialogVisible" :model="data.aduplang" :rules="data.rules" :label-width="language=='en'?'140px':'100px'"
				ref="ruleFormRef">
				<el-form-item :label="$t('manage.language.name') + ':'" prop="name">
					<el-input v-model="data.aduplang.name" :placeholder="$t('message.pleaseEnter')" />
				</el-form-item>
				<el-form-item :label="$t('machine.configs.langCode') + ':'" prop="lang">
					<el-input v-model="data.aduplang.lang" :placeholder="$t('message.pleaseEnter')" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="submit(ruleFormRef)">{{ $t('public.save') }}</el-button>
					<el-button @click="data.dialogVisible = false">{{ $t('public.cancel') }}</el-button>
				</el-form-item>
			</el-form>
		</el-dialog>

	</div>
</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n' 
const { t: $t } = useI18n()
import TopInfo from '@/components/TopInfo/index.vue';
import Search from '@/components/Search/index.vue';
import PageNum from '@/components/PageNum/index.vue';
import getPlevel from '@/utils/level'
import { getlang, addlang, updatelang, dellang } from '@/api/manage';
import { onMounted } from 'vue'
let language = ref()
const { locale } = useI18n()
onMounted(() => {
	language.value = locale.value
})
let ruleFormRef = ref<any>()

let data = reactive({
	mtloading: true, // 加载
	dialogVisible: false, // 弹窗
	lookup: [ // 筛选栏
		{ title: $t('machine.configs.langCode'), type: 'input', value: '', key: 'lang' },
	],
	rules: { // 表单验证
		name: { required: true, message:  $t('message.pleaseEnter'), trigger: 'blur' },
		lang: { required: true, message:  $t('message.pleaseEnter'), trigger: 'blur' },
	},
	list: [], // 列表信息
	lang: { // 筛选数据
		lang: '',
		page: 1,
		pageNum: 15
	},
	total: 0, // 总条数
	aduplang: { // 添加/修改
		name: '',
		lang: '',
	}
})

// 查询列表
let getlangs = () => {
	data.mtloading = true
	getlang(data.lang).then(res => {
		data.mtloading = false
		if (res.state == 200) {
			data.list = res.data.data
			data.total = res.data.total
		}
	}).catch(err => {
		data.mtloading = false
	})
}

// 查询回调
let inves = (e) => {
	for (let key in e) {
		data.lang[key] = e[key]
	}
	data.lang.page = 1
	data.lang.pageNum = 15
	getlangs()
}

// 页数回调
let current = (e) => {
	data.lang.page = e
	getlangs()
}

// 条数回调
let fraction = (e) => {
	data.lang.pageNum = e
	getlangs()
}

// 添加按钮
let add = () => {
	data.aduplang = {
		name: '',
		lang: ''
	}
	data.dialogVisible = true
}

// 修改按钮
let modify = (e) => {
	data.aduplang.l_id = e.l_id
	data.aduplang.name = e.name
	data.aduplang.lang = e.lang
	data.dialogVisible = true
}

// 删除按钮
let del = (e) => {
	ElMessageBox.confirm($t('message.delData'))
		.then(res => {
			dellang({ l_id: e }).then(res => {
				if (res.state == 200) {
					ElMessage({
						message: $t('message.delSuccess'),
						type: 'success'
					})
					data.dialogVisible = false
					getlangs()
					return
				}
				ElMessage({
					message: $t('message.networkFailed'),
					type: 'error'
				})
			})
		}).catch(err => {
			console.log('取消')
		})
}

// 保存按钮
let submit = (formEl: FormInstance | undefined) => {
	if (!formEl) return
	formEl.validate((valid, fields) => {
		if (valid) {
			if (data.aduplang.l_id == undefined) {
				addlang(data.aduplang).then(res => {
					if (res.state == 200) {
						ElMessage({
							message: $t('message.addSuccess'),
							type: 'success'
						})
						data.dialogVisible = false
						getlangs()
						return
					}
					ElMessage({
						message: $t('message.networkFailed'),
						type: 'error'
					})
				})
			} else {
				updatelang(data.aduplang).then(res => {
					if (res.state == 200) {
						ElMessage({
							message: $t('message.modifySuccess'),
							type: 'success'
						})
						data.dialogVisible = false
						getlangs()
						return
					}
					ElMessage({
						message: $t('message.networkFailed'),
						type: 'error'
					})
				})
			}
		}
	})
}


getlangs()




</script>
